#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int n, m;
int a[N][N];
ll dp[N][N];
ll f[N][N], g[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) cin >> a[j][i];
  swap(n, m);
  rep(i, 1, m) dp[1][i] = dp[1][i - 1] + a[1][i];
  rep(i, 2, n) {
    rep(j, 1, m) dp[i][j] = dp[i - 1][j] + a[i][j];  // pushdown from up
    f[i][1] = dp[i][1], g[i][m] = dp[i][m];
    rep(j, 2, m) f[i][j] = max(f[i][j - 1] + a[i][j], dp[i][j]);
    per(j, m - 1, 1) g[i][j] = max(g[i][j + 1] + a[i][j], dp[i][j]);
    rep(j, 1, m) dp[i][j] = max({dp[i][j], f[i][j], g[i][j]});
  }
  cout << dp[n][m];
  return 0;
}